import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class TestKeepAliveWidgetPage extends StatefulWidget {
  @override
  _TestKeepAliveWidgetPageState createState() => _TestKeepAliveWidgetPageState();
}

class _TestKeepAliveWidgetPageState extends State<TestKeepAliveWidgetPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("keep alive list widget"),
      ),
      body: Container(
        child: Container(
          height: 400.h,
          child: ListView.builder(
            itemBuilder: (context, index) => ItemViewFullScreenListView(
              index: index,
            ),
            scrollDirection: Axis.horizontal,
            itemCount: 8,
//          physics: NeverScrollableScrollPhysics(),
          ),
        ),
      ),
    );
  }
}

class ItemViewFullScreenListView extends StatefulWidget {
  final int index;

  const ItemViewFullScreenListView({Key key, this.index}) : super(key: key);

  @override
  _ItemViewFullScreenListViewState createState() => _ItemViewFullScreenListViewState();
}

class _ItemViewFullScreenListViewState extends State<ItemViewFullScreenListView> with AutomaticKeepAliveClientMixin{
  int _data = 1;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Container(
      color: Colors.grey,
      width: MediaQuery.of(context).size.width,
      height: 100.h,
      alignment: Alignment.center,
      child: Center(
        child: Column(
          children: <Widget>[
            Text("index${widget.index}, data == $_data"),
            FlatButton(
              child: Text("change data"),
              onPressed: _changeData,
            )
          ],
        ),
      ),
    );
  }

  void _changeData() {
    setState(() {
      _data = 555;
    });
  }

  @override
  bool get wantKeepAlive => true;
}
